package com.huwenhua.dahufileexplorer;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileExplorer extends AppCompatActivity {
    private ArrayAdapter<String> aaData;
    private static String[] data = new String[] {
            "天地逃生",
            "保持通话",
            "乱世佳人",
            "星际传奇",
            "变形金刚"
    };

    private List<String> items = null;//存放名称
    private List<String> paths = null;//存放路径
    private String rootPath = Environment.getExternalStorageDirectory().getPath();
    private TextView tv;
    private ListView lvCommonListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_explorer);
        aaData = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data);
        tv = (TextView) this.findViewById(R.id.textView1);
        lvCommonListView = (ListView) findViewById(R.id.listView1);
        //lvCommonListView.setAdapter(aaData);
        verifyStoragePermissions();
        lvCommonListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String path = paths.get(position);
                File file = new File(path);
                //如果是文件夹就继续分解
                if(file.isDirectory()){
                    getFileDir(path);
                }/*else{
                    new AlertDialog.Builder(this).setTitle("提示").setMessage(file.getName()+" 是一个文件！").setPositiveButton("OK", new DialogInterface.OnClickListener(){

                        public void onClick(DialogInterface dialog, int which) {

                        }

                    }).show();
                }*/
            }
        });
        this.getFileDir(rootPath);//获取rootPath目录下的文件.
    }

    public  void verifyStoragePermissions() {

        try {
            //检测是否有写的权限
            int permission = this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                this.getStoragePermission(this);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public  void getStoragePermission(Activity activity) {
        activity.requestPermissions(
                new String[] { Manifest.permission.READ_EXTERNAL_STORAGE ,Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
    }

    public void getFileDir(String filePath) {
        try{
            this.tv.setText("当前路径:"+filePath);// 设置当前所在路径
            items = new ArrayList<>();
            paths = new ArrayList<>();
            File f = new File(filePath);
            File[] files = f.listFiles();// 列出所有文件
            // 如果不是根目录,则列出返回根目录和上一目录选项
            if (!filePath.equals(rootPath)) {
                items.add("返回根目录");
                paths.add(rootPath);
                items.add("返回上一层目录");
                paths.add(f.getParent());
            }
            // 将所有文件存入list中
            if(files != null){
                int count = files.length;// 文件个数
                for (int i = 0; i < count; i++) {
                    File file = files[i];
                    items.add(file.getName());
                    paths.add(file.getPath());
                }
            }

            ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                    android.R.layout.simple_list_item_1, items);
            lvCommonListView.setAdapter(adapter);
        }catch(Exception ex){
            ex.printStackTrace();
        }

    }

}
